<?php
class page {
	var $title="";
	var $prefix="";
	var $container;
	var $content=false;

	function page() {
		$this->defaultContainer();
	}

	function setTitle($title = "Default title") {
		$this->title = $title;
		return true;
	}
	
	function setPrefix($prefix = "") {
		$this->prefix = $prefix;
		return true;
	}

	function defaultContainer() {
		$this->passContainer(array("head"=>file_get_contents("lib/technosamurai/classes/default/head.deftemp.php"),
			"footer"=>file_get_contents("lib/technosamurai/classes/default/footer.deftemp.php"),
			"content"=>'<b>{title}</b><br/>{bitty}',
			"contentsep"=>"<br/><br/>"));
	}

	function passContainer($container = false) {
		if (!is_array($container)) {
			error_log("Container passed to page is not array! - ".serialize($container), 0);
			die("Container passed to page is not array!");
		}
		$this->container = $container;
		return true;
	}

	function generateContent() {
		$content=$this->contentCheck();
		$echo;
		if ($content == false||!is_array($content)) {
			error_log("No content sent to generateContent... empty...", 0);
			return "Empty!!! Error!";
		} else {
			$container=$this->container;
			foreach ($this->content as $x => $array) {
				if ($x!=0) {
					$echo.=$container['contentsep'];
				}
				if (isset($array['content'])&&isset($array['title'])) {
					$echo.=str_replace('{bitty}',(string)$array['content'],str_replace('{title}',(string)$array['title'],(string)$container['content']));
				}
			}
			return $echo;
		}
	}

	function contentCheck() {
		if (!is_array($this->content)) $this->content=array(0=>array("title"=>"Meh","content"=>"no meat"));
		return $this->content;
	}

	function addContent($title="No title",$content="No meat") {
		if (!is_array($this->content)) $this->content=array();
		$this->content[]=array("title"=>$title,"content"=>$content);
	}

	function endGen() {
		$container=$this->container;
		$replace=array("title"=>$this->prefix.$this->title);
		$parts=array("head","_PAGE_","footer");
		foreach ($parts as $piece) {
			if ($piece != "_PAGE_") {
				$part = $container[$piece];
				foreach ($replace as $text => $content) {
					$part=str_replace('{'.$text.'}',$content,$part);
				}
				echo $part;
			} else {
				echo $this->generateContent();
			}
		}
	}
}
?>
